home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 16 Instancing and Frustum Culling / InstancingAndCulling / FrameResource.h < prev    next >
Encoding:
C/C++ Source or Header  |  2016-03-02  |  3.9 KB  |  99 lines

  1. #pragma once
  2.  
  3. #include "../../Common/d3dUtil.h"
  4. #include "../../Common/MathHelper.h"
  5. #include "../../Common/UploadBuffer.h"
  6.  
  7. struct InstanceData
  8. {
  9.     DirectX::XMFLOAT4X4 World = MathHelper::Identity4x4();
  10.     DirectX::XMFLOAT4X4 TexTransform = MathHelper::Identity4x4();
  11.     UINT MaterialIndex;
  12.     UINT InstancePad0;
  13.     UINT InstancePad1;
  14.     UINT InstancePad2;
  15. };
  16.  
  17. struct PassConstants
  18. {
  19.     DirectX::XMFLOAT4X4 View = MathHelper::Identity4x4();
  20.     DirectX::XMFLOAT4X4 InvView = MathHelper::Identity4x4();
  21.     DirectX::XMFLOAT4X4 Proj = MathHelper::Identity4x4();
  22.     DirectX::XMFLOAT4X4 InvProj = MathHelper::Identity4x4();
  23.     DirectX::XMFLOAT4X4 ViewProj = MathHelper::Identity4x4();
  24.     DirectX::XMFLOAT4X4 InvViewProj = MathHelper::Identity4x4();
  25.     DirectX::XMFLOAT3 EyePosW = { 0.0f, 0.0f, 0.0f };
  26.     float cbPerObjectPad1 = 0.0f;
  27.     DirectX::XMFLOAT2 RenderTargetSize = { 0.0f, 0.0f };
  28.     DirectX::XMFLOAT2 InvRenderTargetSize = { 0.0f, 0.0f };
  29.     float NearZ = 0.0f;
  30.     float FarZ = 0.0f;
  31.     float TotalTime = 0.0f;
  32.     float DeltaTime = 0.0f;
  33.  
  34.     DirectX::XMFLOAT4 AmbientLight = { 0.0f, 0.0f, 0.0f, 1.0f };
  35.  
  36.     // Indices [0, NUM_DIR_LIGHTS) are directional lights;
  37.     // indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
  38.     // indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
  39.     // are spot lights for a maximum of MaxLights per object.
  40.     Light Lights[MaxLights];
  41. };
  42.  
  43. struct MaterialData
  44. {
  45.     DirectX::XMFLOAT4 DiffuseAlbedo = { 1.0f, 1.0f, 1.0f, 1.0f };
  46.     DirectX::XMFLOAT3 FresnelR0 = { 0.01f, 0.01f, 0.01f };
  47.     float Roughness = 64.0f;
  48.  
  49.     // Used in texture mapping.
  50.     DirectX::XMFLOAT4X4 MatTransform = MathHelper::Identity4x4();
  51.  
  52.     UINT DiffuseMapIndex = 0;
  53.     UINT MaterialPad0;
  54.     UINT MaterialPad1;
  55.     UINT MaterialPad2;
  56. };
  57.  
  58. struct Vertex
  59. {
  60.     DirectX::XMFLOAT3 Pos;
  61.     DirectX::XMFLOAT3 Normal;
  62.     DirectX::XMFLOAT2 TexC;
  63. };
  64.  
  65. // Stores the resources needed for the CPU to build the command lists
  66. // for a frame.  
  67. struct FrameResource
  68. {
  69. public:
  70.     
  71.     FrameResource(ID3D12Device* device, UINT passCount, UINT maxInstanceCount, UINT materialCount);
  72.     FrameResource(const FrameResource& rhs) = delete;
  73.     FrameResource& operator=(const FrameResource& rhs) = delete;
  74.     ~FrameResource();
  75.  
  76.     // We cannot reset the allocator until the GPU is done processing the commands.
  77.     // So each frame needs their own allocator.
  78.     Microsoft::WRL::ComPtr<ID3D12CommandAllocator> CmdListAlloc;
  79.  
  80.     // We cannot update a cbuffer until the GPU is done processing the commands
  81.     // that reference it.  So each frame needs their own cbuffers.
  82.    // std::unique_ptr<UploadBuffer<FrameConstants>> FrameCB = nullptr;
  83.     std::unique_ptr<UploadBuffer<PassConstants>> PassCB = nullptr;
  84.     std::unique_ptr<UploadBuffer<MaterialData>> MaterialBuffer = nullptr;
  85.  
  86.     // NOTE: In this demo, we instance only one render-item, so we only have one structured buffer to 
  87.     // store instancing data.  To make this more general (i.e., to support instancing multiple render-items), 
  88.     // you would need to have a structured buffer for each render-item, and allocate each buffer with enough
  89.     // room for the maximum number of instances you would ever draw.  
  90.     // This sounds like a lot, but it is actually no more than the amount of per-object constant data we 
  91.     // would need if we were not using instancing.  For example, if we were drawing 1000 objects without instancing,
  92.     // we would create a constant buffer with enough room for a 1000 objects.  With instancing, we would just
  93.     // create a structured buffer large enough to store the instance data for 1000 instances.  
  94.     std::unique_ptr<UploadBuffer<InstanceData>> InstanceBuffer = nullptr;
  95.  
  96.     // Fence value to mark commands up to this fence point.  This lets us
  97.     // check if these frame resources are still in use by the GPU.
  98.     UINT64 Fence = 0;
  99. };